package com.edgar.common.lib.config;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.content.SharedPreferencesCompat;

import com.edgar.baselibrary.AppGlobalContext;
import com.edgar.baselibrary.utils.AppUtils;

/**
 * Created by edgar on 2015/12/22.
 * 通用的配置类
 */
public final class CommonConfig {

    private static final String TAG = "CommonConfig";
    /** 通用配置文件名 **/
    private static final String COMMON_CONFIG = "common_config";
    private static final String COMMON_SAVE_VERSION_CODE = "save_version_code";

    private static SharedPreferences mCommonPreferences;

    private static void initialize(){
        if(mCommonPreferences == null){
            Context context = AppGlobalContext.getInstance().getApplicationContext();
            mCommonPreferences = context.getSharedPreferences(COMMON_CONFIG,Context.MODE_APPEND);
        }
    }
    /**
     * 获取SharePreferences中存储的版本号
     * @return
     */
    public static int getStorageVersionCode(){
        initialize();
        return mCommonPreferences.getInt(COMMON_SAVE_VERSION_CODE,0);
    }

    /**
     * 存储当前App版本号
     */
    public static void saveVersionCode(){
        initialize();
        Context context = AppGlobalContext.getInstance().getApplicationContext();
        SharedPreferences.Editor editor = mCommonPreferences.edit();
        editor.putInt(COMMON_SAVE_VERSION_CODE, AppUtils.getAppCurrentVersionCode(context));
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
    }
}